﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


[RequireComponent(typeof(Text))]
public class NumShow : MonoBehaviour {

    public Text m_NumText;
    public Image icon;
    public Action<float> onNumUpdate;
     
    float m_CurShowNum;
    float m_Num = -1;
    public float num
    {
        get
        {
            return m_Num;
        }
        set
        {
            if (value != m_Num)
            {
                float delta = value - m_Num;
                float speed = delta < 30 ? 1 : delta / 30;
                m_Num = value;
                
                if (!this) return;
                
                if ( gameObject.activeInHierarchy)
                {
                    StartCoroutine(PlayAddToNum(m_CurShowNum, m_Num, speed));
                }
                else
                {
                    SetText(m_Num);
                }

                m_CurShowNum = m_Num;
            }
        }
    }
    string prefix = "";
    string suffix = "";
    int m_Round;

    public IPlayLoopAudio audioPlay { get; set; }

    void Awake()
    {
        if(m_NumText == null)
            m_NumText = GetComponent<Text>();

        if(audioPlay == null)
            audioPlay = new NumShowAudioPlay();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="num">初始数字</param>
    /// <param name="prexStr">前缀</param>
    /// <param name="round">精度</param>
    public void Init(float num,string prexStr, string suffStr, int round)
    {
        this.m_Num = num;
        this.m_CurShowNum = num;
        prefix = prexStr;
        suffix = suffStr;
        m_Round = round;

        SetText(num);
    }

	// Use this for initialization
	void Start () {
		
	}

    private void OnDestroy()
    {
        if (audioPlay != null)
            audioPlay.StopLoopAudio(audioPlay.toPlayAudio);
    }

    public void SetTextColor(Color color)
    {
        this.m_NumText.color = color;
    }

    IEnumerator PlayAddToNum(float curNum,float targetNum,float speed)
    {
        if(audioPlay != null)
            audioPlay.PlayLoopAudio(audioPlay.toPlayAudio);

        while (curNum < targetNum)
        {
            curNum += speed;

            SetText((int)curNum);

            onNumUpdate?.Invoke(curNum);
            yield return 0;
        }
        SetText(targetNum);
        onNumUpdate?.Invoke(targetNum);
        
        if (audioPlay != null)
            audioPlay.StopLoopAudio(audioPlay.toPlayAudio);
    }

    public int testAddNum = 100;
    [ContextMenu("TestAdd10")]
    void TestAdd10()
    {
        this.num += testAddNum;
    }

    void SetText(float num)
    {
        SetText(num,this.prefix,this.suffix,this.m_Round);
    }


    void SetText(float num,string prefix,string suffix,int round)
    {
        string  numStr = GetNumShowStr(num);
        if (m_NumText)
            m_NumText.text = prefix + numStr + suffix;
    }

    public static string GetNumShowStr(float num)
    {
        /*bool m = num >= 1000;

        if (m)
        {
            num /= 1000;
        }*/
        
        string numStr = ((int)num).ToString();
        
        int power = (int)Mathf.Pow(10, 1);
        int p = (int)(num * power);
        numStr =  ((float)p / power).ToString();
        
        /*if (m)
        {
            numStr += "M";
        }
        else
        {
            numStr += "K";
        }*/

        return numStr;
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
